// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TranslationManager.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the <seealso cref="TranslationManager" /> type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Client.Common.Translation
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Threading;

    /// <summary>
    /// Defines the <seealso cref="TranslationManager"/> type.
    /// </summary>
    public class TranslationManager
    {
        /// <summary>
        /// Member variable _translationManager
        /// </summary>
        private static TranslationManager _translationManager;

        /// <summary>
        /// Occurs when [language changed].
        /// </summary>
        public event EventHandler LanguageChanged;

        /// <summary>
        /// Gets the instance.
        /// </summary>
        public static TranslationManager Instance
        {
            get
            {
                if (_translationManager == null)
                {
                    _translationManager = new TranslationManager();
                }

                return _translationManager;
            }
        }

        /// <summary>
        /// Gets or sets the current language.
        /// </summary>
        /// <value>
        /// The current language.
        /// </value>
        public CultureInfo CurrentLanguage
        {
            get
            {
                return Thread.CurrentThread.CurrentUICulture;
            }

            set
            {
                if (value != Thread.CurrentThread.CurrentUICulture)
                {
                    Thread.CurrentThread.CurrentUICulture = value;
                    OnLanguageChanged();
                }
            }
        }

        /// <summary>
        /// Gets the languages.
        /// </summary>
        public IEnumerable<CultureInfo> Languages
        {
            get
            {
                if (TranslationProvider != null)
                {
                    return TranslationProvider.Languages;
                }

                return Enumerable.Empty<CultureInfo>();
            }
        }

        /// <summary>
        /// Gets or sets the translation provider.
        /// </summary>
        /// <value>
        /// The translation provider.
        /// </value>
        public ITranslationProvider TranslationProvider { get; set; }

        /// <summary>
        /// Translates the specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns>Translated object</returns>
        public object Translate(string key)
        {
            if (TranslationProvider != null)
            {
                object translatedValue = TranslationProvider.Translate(key);
                if (translatedValue != null)
                {
                    return translatedValue;
                }
            }

            return string.Format("!{0}!", key);
        }

        /// <summary>
        /// Called when [language changed].
        /// </summary>
        private void OnLanguageChanged()
        {
            if (LanguageChanged != null)
            {
                LanguageChanged(this, EventArgs.Empty);
            }
        }
    }
}